import http from "@/api";

// 产线相关接口
export namespace ProductionLine {
  // 创建产线请求参数
  export interface CreateProductionLineRequestDto {
    lineCode: string;
    lineName: string;
    factoryId: string; // 改为string类型避免精度丢失
    workshopId: string; // 改为string类型避免精度丢失
    lineType: string;
    status?: string;
    maxDeviceCapacity?: number;
    supportedProductTypes?: string;
    description?: string;
  }

  // 更新产线请求参数
  export interface UpdateProductionLineRequestDto {
    lineName: string;
    factoryId: string; // 改为string类型避免精度丢失
    workshopId: string; // 改为string类型避免精度丢失
    lineType: string;
    status?: string;
    maxDeviceCapacity?: number;
    supportedProductTypes?: string;
    description?: string;
  }

  // 产线列表项
  export interface ProductionLineListItem {
    id: string;
    lineCode: string;
    lineName: string;
    factoryId: string; // 改为string类型避免精度丢失
    factoryName: string;
    workshopId: string; // 改为string类型避免精度丢失
    workshopName: string;
    lineType: string;
    status: string;
    location: string;
    maxDeviceCapacity?: number;
    supportedProductTypes?: string;
    description?: string;
    createTime: string;
    updateTime: string;
  }

  // 产线列表响应
  export interface ProductionLineListResponse {
    records: ProductionLineListItem[];
    total: number;
  }
}

/** 创建产线 */
export const createProductionLine = (params: ProductionLine.CreateProductionLineRequestDto) => {
  return http.post<void>("/productionLine/create", params);
};

/** 获取产线列表 */
export const getProductionLineList = (params?: {
  lineCode?: string;
  lineName?: string;
  factoryId?: string; // 改为string类型避免精度丢失
  workshopId?: string; // 改为string类型避免精度丢失
  status?: string;
  lineType?: string;
  current?: number;
  size?: number;
}) => {
  return http.get<ProductionLine.ProductionLineListResponse>("/productionLine/list", params);
};

/** 更新产线 */
export const updateProductionLine = (id: string, params: ProductionLine.UpdateProductionLineRequestDto) => {
  return http.put<void>(`/productionLine/${id}`, params);
};

/** 删除产线 */
export const deleteProductionLine = (id: string) => {
  return http.delete<void>(`/productionLine/${id}`);
};

/** 批量删除产线 */
export const batchDeleteProductionLine = (ids: string[]) => {
  return http.delete<void>("/productionLine/batch", { data: { ids } });
};

/** 获取启用状态的工厂列表 */
export const getEnabledFactoryList = () => {
  return http.get<{ id: string; factoryName: string }[]>("/factories/status/enabled");
};

/** 根据工厂ID获取启用状态的车间列表 */
export const getEnabledWorkshopsByFactoryId = (factoryId: string) => {
  return http.get<{ id: string; workshopName: string }[]>(`/workshops/factory/${factoryId}/enabled`);
};

/** 获取产线详情 */
export const getProductionLineById = (id: string) => {
  return http.get<ProductionLine.ProductionLineListItem>(`/productionLine/${id}`);
};